﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Diagnostics;
using Microsoft.Win32;

namespace TuiXin
{


    public class AtomBase
    {
        public LogFile log = new LogFile();

        private int _Flag;
        public int Flag
        {
            get
            {
                return (this._Flag);
            }
            set
            {
                this._Flag = value;
            }
        }

        private bool _IsRun = true;
        public bool IsRun
        {
            get
            {
                return (this._IsRun);
            }
            set
            {
                this._IsRun = value;
            }
        }

        private int _Index;
        public int Index
        {
            get
            {
                return (this._Index);
            }
            set
            {
                this._Index = value;
            }
        }


        public AtomBase()
        {
        }
    }

    public class LogFile
    {

        // log\Date(2006-08-15)\*.log
        private bool _ByDate = false;
        public bool ByDate
        {
            set
            {
                this._ByDate = value;
            }
        }

        // log\Date(2006-08-15)\Hour(10)\*.log
        private bool _ByHour = false;
        public bool ByHour
        {
            set
            {
                this._ByHour = value;
            }
        }

        private string _FilePath;
        private string _TextFilePath;

        private string _DataFilePath;

        public LogFile()
        {

            string _Path = System.IO.Directory.GetCurrentDirectory();
            this._FilePath = _Path + @"\Log\";
            if (!Directory.Exists(this._FilePath))
            {
                Directory.CreateDirectory(this._FilePath);
            }

            this._TextFilePath = _Path + @"\Log\Process\";
            if (!Directory.Exists(this._TextFilePath))
            {
                Directory.CreateDirectory(this._TextFilePath);
            }

            this._DataFilePath = _Path + @"\Log\Data\";
            if (!Directory.Exists(this._DataFilePath))
            {
                Directory.CreateDirectory(this._DataFilePath);
            }

        }


        public void Bytes(string FileName, byte[] bs)
        {
            try
            {
                lock (this)
                {
                    StreamWriter sr;
                    FileName = this._FilePath + FileName + ".log";

                    if (File.Exists(FileName))
                    {
                        sr = File.AppendText(FileName);
                    }
                    else
                    {
                        sr = File.CreateText(FileName);
                    }
                    sr.WriteLine("---------------------------");
                    sr.WriteLine(DateTime.Now.ToString());
                    sr.WriteLine("Length=" + bs.Length.ToString());
                    sr.WriteLine("");
                    for (int i = 0; i < bs.Length; i++)
                    {
                        byte b = bs[i];
                        sr.Write(b.ToString("X").PadLeft(2, '0') + " ");
                    }
                    sr.WriteLine("");
                    sr.WriteLine("---------------------------");
                    sr.Close();
                }
            }
            catch
            {
            }
        }

        public void Msg(string FileName, string Msg)
        {
            try
            {
                lock (this)
                {
                    StreamWriter sr;
                    FileName = this._FilePath + FileName + ".log";

                    if (File.Exists(FileName)) //如果文件存在,则创建File.AppendText对象
                    {
                        sr = File.AppendText(FileName);
                    }
                    else  //如果文件不存在,则创建File.CreateText对象
                    {
                        sr = File.CreateText(FileName);
                    }
                    Msg = GetDateTime() + "," + Msg;
                    sr.WriteLine(Msg);
                    sr.Close();
                }
            }
            catch
            {
            }
        }

        public void SQL(string FileName, string SQL)
        {
            try
            {
                lock (this)
                {
                    StreamWriter sr;
                    FileName = this._FilePath + FileName + ".txt";

                    if (File.Exists(FileName))
                    {
                        sr = File.AppendText(FileName);
                    }
                    else
                    {
                        sr = File.CreateText(FileName);
                    }

                    sr.WriteLine(SQL);
                    sr.Close();
                }

            }
            catch (Exception err)
            {

            }
        }

        public void Text(string FileName, string Msg)
        {
            try
            {
                lock (this)
                {
                    StreamWriter sr;
                    FileName = this._TextFilePath + FileName + ".txt";

                    if (File.Exists(FileName))
                    {
                        sr = File.AppendText(FileName);
                    }
                    else
                    {
                        sr = File.CreateText(FileName);
                    }

                    sr.WriteLine(Msg);
                    sr.Close();
                }

            }
            catch (Exception err)
            {

            }
        }
        public void Record(string FileName, string record)
        {
            StreamWriter sr;

            if (File.Exists(FileName))
            {
                sr = File.AppendText(FileName);
            }
            else
            {
                sr = File.CreateText(FileName);
            }

            sr.WriteLine(record);
            sr.Close();
        }
        public void DelFile(string FileName)
        {
            File.Delete(FileName);
        }
        public void Data(string FileName, string Msg)
        {
            try
            {
                lock (this)
                {
                    StreamWriter sr;
                    FileName = this._DataFilePath + FileName + ".txt";

                    if (File.Exists(FileName))
                    {
                        sr = File.AppendText(FileName);
                    }
                    else
                    {
                        sr = File.CreateText(FileName);
                    }

                    sr.WriteLine(Msg);
                    sr.Close();
                }

            }
            catch (Exception err)
            {

            }
        }
        private string GetDateTime()
        {
            System.DateTime CurrentTime = new System.DateTime();
            CurrentTime = System.DateTime.Now;
            return CurrentTime.ToString();
        }

    }


}
